package 教学管理系统;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.*;
class Register {
    String name;
    String password;
    String confirmpassword;
    ResultSet rs = null;
    private String driver = "com.mysql.jdbc.Driver";
    private String url = "jdbc:mysql://localhost:3306/InfoDb?serverTimezone=UTC&characterEncoding=gbk";
    private String userMySql = "root";
    private String passwordMySql = "123456";

    void setName(String name) {
        this.name = name;
    }

    void setPassword(String password) {
        this.password = password;
    }
    void setconfirmpasswd(String confirmpassword) {
        this.confirmpassword = confirmpassword;
    }


    //判断注册的账号是否符合规则
    boolean JudgeRegister()  {

        if(this.name.equals("")) {
            JOptionPane.showMessageDialog(null, "用户名不能为空！", "用户名", JOptionPane.ERROR_MESSAGE);
            return false;
        }


        if(this.password.equals("")) {
            JOptionPane.showMessageDialog(null, "密码不能为空！", "密码为空", JOptionPane.ERROR_MESSAGE);
            return false;
        }

        if(!this.password.equals(this.confirmpassword)) {
            JOptionPane.showMessageDialog(null, "两次输入的密码不一致!", "密码不一致", JOptionPane.ERROR_MESSAGE);
            return false;
        }

        //符合规则，弹出注册成功的窗口，并将账号添加数据库
        JOptionPane.showMessageDialog(null, "注册成功");
        addAdmin();
        return true;
    }

    //向数据库添加Admin账户
    void addAdmin()  {
        String sql="insert into userpass  values ('"+name+"','"+ password+"')";
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        try {
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/InfoDb?user="
                    + userMySql + "&password=" + passwordMySql + "&useUnicode=true&characterEncoding=gbk&useSSL=false&serverTimezone=GMT");
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.executeUpdate();
            ps.close();
            conn.close();

        }catch(SQLException ex) {
            System.out.println("添加用户失败！");
        }

    }
}
public class zhuce extends JFrame {
    zhuce() {
        init();
    }

    void init() {
        JFrame frame = new JFrame("注册管理员账号");
        frame.setLayout(null);

        JLabel nameStr = new JLabel("用户名:");
        nameStr.setBounds(20, 20, 60, 25);
        frame.add(nameStr);

        JLabel passwordStr = new JLabel("密码:");
        passwordStr.setBounds(20, 50, 60, 25);
        frame.add(passwordStr);

        JLabel confrimStr = new JLabel("确认密码:");
        confrimStr.setBounds(20, 80, 60, 30);
        frame.add(confrimStr);

        JTextField userName = new JTextField();
        userName.setBounds(100, 20, 150, 25);
        frame.add(userName);

        JPasswordField password = new JPasswordField();
        password.setBounds(100, 50, 150, 25);
        frame.add(password);

        JPasswordField confrimPassword = new JPasswordField();
        confrimPassword.setBounds(100, 80, 150, 25);
        frame.add(confrimPassword);

        JButton buttonregister = new JButton("注册");
        buttonregister.setBounds(120, 120, 70, 25);
        frame.add(buttonregister);

        frame.setLocation(550, 250);
        frame.setSize(300, 200);
        frame.setVisible(true);

        confrimPassword.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e2) {
                if (e2.getKeyChar() == KeyEvent.VK_ENTER) {//如果密码是enter键
                    buttonregister.doClick();//点击登录按钮
                }
            }
        });

//        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        frame.setVisible(true);
        //为注册按钮增加监听器
        buttonregister.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name = userName.getText();
                String passwd = new String(password.getPassword());
                String confrimpasswd = new String(confrimPassword.getPassword());

                //创建Register类
                Register register = new Register();
                register.setName(name);
                register.setPassword(passwd);
                register.setconfirmpasswd(confrimpasswd);

                //如果注册成功，返回登录界面
                if (register.JudgeRegister()) {
                    frame.setVisible(false);
                }

            }

        });

    }
}
